﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

using AppDialogBox;

namespace PasswordControl
{
    public partial class LoadProfile_Form : Form
    {
        /*
         * Variables 
         */
        private ControlProgram m_controlform;

        // confirm if data is being loaded - true if load button was clicked
        private bool m_bConfirm = false;

        /*
         * Methods
         */ 
        // constructor & load 
        public LoadProfile_Form(ControlProgram controlform)
        {
            InitializeComponent();

            this.m_controlform = controlform;
        }
        private void LoadProfile_Form_Load(object sender, EventArgs e)
        {
            this.Activate();
            this.LoadBox.Focus();

            //add profiles to profile box
            string sourceDir = ControlProgram.PROFILES_PATH;
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {
                if (Path.GetExtension(fileName).CompareTo(ControlProgram.PASSWORD_EXTENTION) != 0) continue;
                this.LoadBox.Items.Add(Path.GetFileNameWithoutExtension(fileName));
            }

            if (this.LoadBox.Items.Count > 0)
            {
                this.LoadBox.SelectedIndex = 0;
            }
        }
        

        //load functions
        private void LoadProfile_Click(object sender, EventArgs e)
        {
            if (this.LoadBox.Text.Length == 0)
            {
                AppDialogWrapper.PromptBox("Profile name must be chosen", ControlProgram.MESSAGE_ERROR);
                return;
            }
            this.Enabled = false;
            this.m_bConfirm = true;

            List<Owners> data = LoadData();
            if (data == null)
            {
                this.CancelLoad_Click(sender, e);
                return;
            }

            m_controlform.InitProfile(this.LoadBox.Text);
            m_controlform.InitLists(data,true);

            this.LoadProfile_Finish();
        }
        private List<Owners> LoadData()
        {
            string path = ControlProgram.GetProfilePath(this.LoadBox.Text, false);

            List<Owners> data;
            if ((data = DataSerialize.LoadEncrypted<List<Owners>>(path, this.TextPassword.Text)) != null)
            {
                m_controlform.CurrentPassword = this.TextPassword.Text;
            }

            return data;
        }

        //cancel button
        private void CancelLoad_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.m_bConfirm = false;

            m_controlform.InitLists();

            this.LoadProfile_Finish();
        }
        //new button
        private void NewProfile_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.m_bConfirm = false;

            this.m_controlform.MenuNew_Click(sender, e);

            this.LoadProfile_Finish();
        }

        //finalize function
        private void LoadProfile_Finish()
        {
            this.Hide();
            if (this.m_bConfirm)
            {
                this.m_controlform.ListChanged = false;
                this.m_controlform.labelSaved.Visible = false;
                this.m_controlform.ProfileLabel = this.LoadBox.Text;
            }

            if (m_controlform.WindowState == FormWindowState.Minimized) this.m_controlform.WindowState = FormWindowState.Normal;
            this.m_controlform.Activate();
        }

        //check if there are saved profiles
        static public bool CheckSavedProfiles()
        {
            string sourceDir = ControlProgram.PROFILES_PATH;
            string[] fileEntries = Directory.GetFiles(sourceDir);
            if (fileEntries.Length > 0)
                return true;
            else
                return false;
        }
    }
}
